package com.example.mybatisplus.common.utls;

//import com.example.mybatisplus.model.domain.UserInfo;
import com.example.mybatisplus.model.domain.Userlist;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionUtils {
    private static final String USERKEY = "loginUser";

    public static HttpSession session() {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        return attr.getRequest().getSession(true); // true == allow create
    }
    public static void saveCurUser(Userlist login) {
        HttpSession session = session();
        session.setAttribute(USERKEY,login);
    }

    public static Userlist getCurUser() {
        HttpSession session = session();
        Object loginUser = session.getAttribute(USERKEY);
        return (Userlist)loginUser;

//        return (Userlist) session().getAttribute(USERKEY);

    }

    public static void removeCurUser() {
        HttpSession session = session();
        session.removeAttribute(USERKEY);
    }

//    public static UserInfo getUserBySession(HttpServletRequest request) {
//        HttpSession session = request.getSession(false);
//        UserInfo userInfo = null;
//        if (session != null && (userInfo = (UserInfo) session.getAttribute(AppFinal.USERINFO_SESSION_KEY)) != null) {
//        }
//        return userInfo;
//    }
}
